<template>
  <div class="person">
    <h1>情况二：监视【ref】定义的【对象类型数据】</h1>
    <h2>姓名：{{ person.name }}</h2>
    <h2>年龄：{{ person.age }}</h2>
    <button @click="changeName">修改姓名</button>
    <button @click="changeAge">修改年龄</button>
    <button @click="changePerson">修改人</button>
  </div>
</template>

<script lang="ts" setup name="Person">
import { ref, watch } from 'vue'

let person = ref({ name: '张三', age: 20 })

function changeName() {
  person.value.name += '~'
}

function changeAge() {
  person.value.age += 1
}

function changePerson() {
  person.value = { name: '李四', age: 50 }
}

watch(person, (newValue, oldValue) => {
  console.log('Person发生了变化', newValue, oldValue)
}, { deep: true, immediate: true })

</script>


<style scoped>
.person {
  background-color: skyblue;
  box-shadow: 0 0 10px;
  border-radius: 10px;
  padding: 20px;
}

button {
  margin: 0 5px;
}
</style>